package com.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.entity.Role;
import com.entity.RolePermission;
import com.entity.User;
import com.entity.UserPermission;
import com.service.RolePermissionService;
import com.service.RoleService;
import com.util.ReturnResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/api")
public class RoleController{
    
    @Resource
    private RoleService service;
    
    @Resource
    private RolePermissionService rolePermissionService;
    
    @RequestMapping("/role/selectName/{id}")
    @ResponseBody
    public ReturnResult selectName(@PathVariable("id") int id){
        ReturnResult result = new ReturnResult();
        Role role = service.selectById(id);
        result.returnSuccess(role,"查询成功！");
        return result;
    }
    
    @RequestMapping("/role/selectList")
    @ResponseBody
    public ReturnResult selectList(){
        ReturnResult result = new ReturnResult();
        List<Role> roleList = service.selectList(null);
        result.returnSuccess(roleList,"查询成功！");
        return result;
    }
    
    @RequestMapping("/role/selectList/{page}/{limit}")
    @ResponseBody
    public ReturnResult selectList(@PathVariable("page") int page,@PathVariable("limit") int limit){
        ReturnResult result = new ReturnResult();
        Page<Role> pageConfig = new Page<>(page,limit);
        pageConfig.setTotal(service.selectCount(null));
        Page<Role> rolePage = service.selectPage(pageConfig,null);
        result.returnSuccess(rolePage,"查询成功！");
        return result;
    }
    
    @RequestMapping("/role/add/{name}/{arrStr}")
    @ResponseBody
    public ReturnResult add(@PathVariable("name") String name,@PathVariable("arrStr") String arrStr){
        ReturnResult result = new ReturnResult();
        Role r = new Role();
        r.setName(name);
        boolean result_insert = service.insert(r);
        if (result_insert) {
            r = service.selectOne(new EntityWrapper<Role>().eq("name",name));
            String cutStr = ",";
            String[] newStr = arrStr.split(cutStr);
            int[] roleIdArr = Arrays.stream(newStr).mapToInt(Integer::parseInt).toArray();
            List<Integer> list = Arrays.stream(roleIdArr).boxed().collect(Collectors.toList());
            List<RolePermission> permissionList = new ArrayList<>();
            for(int i : list){
                permissionList.add(new RolePermission(r.getId(),i));
            }
            boolean result_insertRolePermission = rolePermissionService.insertBatch(permissionList);
            if(result_insertRolePermission)
                result.returnSuccess(result_insertRolePermission,"新增成功！");
            else
                result.returnFail("新增成功，分配权限失败！");
        }else{
            result.returnFail("新增失败！");
        }
        return result;
    }
    
    @RequestMapping("/role/selectOne/{id}")
    @ResponseBody
    public ReturnResult selectOne(@PathVariable("id") int id){
        ReturnResult result = new ReturnResult();
        Role r = service.selectById(id);
        if(r != null)
            result.returnSuccess(r,"查询成功！");
        else
            result.returnFail("查询失败！");
        return result;
    }
    
    @RequestMapping("/role/upd")
    @ResponseBody
    public ReturnResult upd(@RequestBody Role role){
        ReturnResult result = new ReturnResult();
        boolean result_Upd = service.updateById(role);
        if(result_Upd)
            result.returnSuccess(result_Upd,"修改成功！");
        else 
            result.returnFail("修改失败！");
        return result;
    }
    
    @RequestMapping("/role/del/{id}")
    @ResponseBody
    public ReturnResult del(@PathVariable("id") int id){
        ReturnResult result = new ReturnResult();
        boolean result_DelPermission = rolePermissionService.delete(new EntityWrapper<RolePermission>().eq("roleId",id));
        if(result_DelPermission){
            boolean result_DelRole = service.deleteById(id);
            if(result_DelRole)
                result.returnSuccess(result_DelRole,"删除成功！");
            else 
                result.returnFail("删除权限成功，删除角色失败！");
        }else {
            result.returnFail("删除失败！");
        }
        return result;
    }
    
    @RequestMapping("/role/selectPermission/{id}")
    @ResponseBody
    public ReturnResult selectPermission(@PathVariable("id") int id){
        ReturnResult result = new ReturnResult();
        result.returnSuccess(rolePermissionService.selectList(new EntityWrapper<RolePermission>().eq("roleId",id)),"查询成功！");
        return result;
    }
    
    @RequestMapping("/role/doAssign/{id}/{array}")
    @ResponseBody
    public ReturnResult doAssign(@PathVariable("id") int id,@PathVariable("array") String str){
        ReturnResult result = new ReturnResult();
        String cutStr = ",";
        String[] newStr = str.split(cutStr);
        boolean delResult = rolePermissionService.delete(new EntityWrapper<RolePermission>().eq("roleId",id));
        if(delResult){
            boolean addResult = false;
            for(String roleId_Str:newStr){
                int i = Integer.parseInt(roleId_Str);
                addResult = rolePermissionService.insert(new RolePermission(id,i));
            }
            result.returnSuccess(addResult,"权限分配成功！");
        } else{
            result.returnFail("权限分配失败！");
        }
        return result;    
    }
}
